package com.ycfmedical.bed.wifi;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.ycfmedical.bed.Constants;
import com.ycfmedical.bed.ui.LoadingActivity;
import com.ycfmedical.bed.utils.ConvertOrderCheckoutUtil;

import android.os.Handler;
import android.os.Message;

import cn.droidlover.xdroidmvp.log.XLog;

/**
 * Created by Administrator on 2017-03-27.
 */

public class WifiEvent {
    /** Called when the activity is first created. */


    // 静态实例变量
    private static WifiEvent wifiEvent;
    //私有化构造函数
    private WifiEvent() {}
    private ClientThread clientThread = null;

    //没有用到的参数
    Message msg;
    static final int maxNode = 4;
    static final int rxDataUpdateUi = 1;
    private Message MainMsg;
    public static Handler mainHandler;
    static final int tipsUpdateUi = 3;   //tips_update_ui
    static final int seatUpdateUi = 6;   //seat_update_ui
    int count =0;

    //连接WiFi中继器
    public void getconnect(String ipAddressed,int comPortOne)
    {
        boolean isIpCheck = isIPAddress(ipAddressed);
        if(isIpCheck)
        {
            XLog.e("SOCKET","重新连接服务器，默认为接收数据流程");
            clientThread = ClientThread.getInstance();
            clientThread.receiveOrderMsg();
            clientThread.start();
        }
    }
    //ip地址格式判断
    private boolean isIPAddress(String ipAddress) {

        boolean isIpCheckOut;
        Pattern pattern = Pattern
                .compile("\\b((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\b");
        Matcher matcher = pattern.matcher(ipAddress);
        isIpCheckOut = matcher.matches();
        return isIpCheckOut;

    }

    //发送数据
    public void senddata(String orderMessage)
    {
        String orderMsg = ConvertOrderCheckoutUtil.instructCheckOut(orderMessage);
        clientThread = ClientThread.getInstance();
        clientThread.sendOrderMsg(orderMessage);
        XLog.e("SOCKET","开始发送数据流程");
        clientThread.start();
    }

    //使用获取到类的单例
    public static WifiEvent getInstance()
    {
        if (wifiEvent == null)
        {
            wifiEvent = new WifiEvent();
        }
        return wifiEvent;
    }


//    void initMainHandler() {
//        mainHandler = new Handler() {
//
//            public void handleMessage(Message msg) {
//                switch (msg.what) {
//                    case tipsUpdateUi:
//                        String strTips = (String) msg.obj;
//
//                        break;
//                    case seatUpdateUi:
//                        String strSeat = (String) msg.obj;
//                        Log.e("msg ==", ""+msg);
//                        if(count<70)
//                        {
//                            count++;
//
//                        }
//                        else {
//                            count =0;
//                        }
//                        break;
//                }
//                super.handleMessage(msg);
//            }
//        };
//    }

}
